package com.eastfair.certificate.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.tenant.TenantAuth;
import com.eastfair.certificate.dto.*;
import com.eastfair.certificate.entity.BadgeTemplate;
import com.eastfair.certificate.entity.CertificateUser;
import com.eastfair.certificate.service.BadgeTemplateService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.certificate.vo.BadgeTemplateVO;
import com.eastfair.certificate.vo.CertificateIndexVO;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 证件模块
 * </p>
 *
 * @author ligang
 * @date 2022-12-20
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/badgeTemplate")
@Api(value = "BadgeTemplate", tags = "证件模块")
@PreAuth(replace = "certificate:badgeTemplate:")
public class BadgeTemplateController extends SuperController<BadgeTemplateService, Long, BadgeTemplate, BadgeTemplatePageQuery, BadgeTemplateSaveDTO, BadgeTemplateUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<BadgeTemplate> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<BadgeTemplate> badgeTemplateList = list.stream().map((map) -> {
            BadgeTemplate badgeTemplate = BadgeTemplate.builder().build();
            //TODO 请在这里完成转换
            return badgeTemplate;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(badgeTemplateList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BadgeTemplatePageQuery> params) {
        BadgeTemplatePageQuery pageQuery = params.getModel();

    }


    /**
     * 添加或更新模板数据
     *
     * @param badgeTemplateDTO
     * @return
     */
    @ApiOperation(value = "添加或更新模板数据", notes = "添加或更新模板数据")
    @PostMapping("/saveOrUpdTemplate")
    @BaseControllerInfo()
    public R<BadgeTemplateVO> saveOrUpdTemplate(@RequestBody JSONObject badgeTemplateDTO) {
        BadgeTemplateDTO badgeTemplateDTO2 = JSONObject.parseObject(badgeTemplateDTO.toJSONString(), BadgeTemplateDTO.class);
        badgeTemplateDTO.remove("badgeDetailJsonList");
        badgeTemplateDTO.remove("id");
        badgeTemplateDTO.remove("certificateId");
        badgeTemplateDTO.remove("templateName");
        badgeTemplateDTO.remove("templateType");


        badgeTemplateDTO2.setHigh(badgeTemplateDTO.getInteger("high"));
        badgeTemplateDTO2.setWide(badgeTemplateDTO.getInteger("wide"));
        badgeTemplateDTO2.setTypeface(badgeTemplateDTO.getString("typeface"));
        badgeTemplateDTO2.setEnTypeface(badgeTemplateDTO.getString("enTypeface"));
        badgeTemplateDTO.remove("high");
        badgeTemplateDTO.remove("wide");
        badgeTemplateDTO.remove("typeface");
        badgeTemplateDTO.remove("enTypeface");
        badgeTemplateDTO.remove("lineBreak");
        badgeTemplateDTO2.setStyleTxt(badgeTemplateDTO.toJSONString());

        //查询是否存在
        if (badgeTemplateDTO2.getId() == null) {
            BadgeTemplateDTO badgeTemplateDTO3 = new BadgeTemplateDTO();
            badgeTemplateDTO3.setClassify(badgeTemplateDTO2.getClassify());
//            badgeTemplateDTO3.setCertificateId(badgeTemplateDTO2.getCertificateId());
//            badgeTemplateDTO3.setExhibitionId(badgeTemplateDTO2.getExhibitionId());
            badgeTemplateDTO3.setCertificateType(badgeTemplateDTO2.getCertificateType());
            List<BadgeTemplate> badgeTemplateList = baseService.list(badgeTemplateDTO3, null);
            if (CollectionUtil.isNotEmpty(badgeTemplateList)) {
                return R.fail("同类数据已存在");
            }
        }
        return R.success(baseService.saveOrUpdTemplate(badgeTemplateDTO2));
    }


    /**
     * 模板分页
     *
     * @param params 证件指标实体
     * @return
     */
    @ApiOperation(value = "模板分页", notes = "模板分页")
    @PostMapping("/pageVo")
    public R<IPage<JSONObject>> pageVo(@RequestBody PageParams<BadgeTemplateDTO> params) {
        IPage<BadgeTemplateVO> page = baseService.pageVo(params);
        //转换对象
        List<JSONObject> jsonObjectList = baseService.assembleDataVO(page.getRecords());
        IPage<JSONObject> jsonObjectIPage = ConvertUtil.convertIPage(page, jsonObjectList);
        return R.success(jsonObjectIPage);
    }

    /**
     * 详情
     *
     * @param id 模板id
     * @return
     */
    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/detail/{id}")
    public R<JSONObject> detail(@PathVariable("id") Long id) {
        BadgeTemplateVO badgeTemplateVO = baseService.detail(id);
        List<JSONObject> jsonObjectList = baseService.assembleDataVO(Arrays.asList(badgeTemplateVO));
        if (CollectionUtil.isEmpty(jsonObjectList)) {
            return R.success(null);
        }
        return R.success(jsonObjectList.get(0));
    }


    /**
     * 获取打印数据
     *
     * @return
     */
    @PostMapping("getPrintData")
    public R<JSONObject> getPrintData(@RequestBody BadgeTemplateDTO badgeTemplateDTO) {
        log.info("getPrintData is execute ");
        BadgeTemplateVO badgeTemplateVO = baseService.getPrintData(badgeTemplateDTO);
        log.info("badgeTemplateVO is {} ", JSONObject.toJSONString(badgeTemplateVO));
        List<JSONObject> jsonObjectList = baseService.assembleDataVO(Arrays.asList(badgeTemplateVO));
        if (CollectionUtil.isEmpty(jsonObjectList)) {
            return R.success(null);
        }
        log.info("jsonObjectList is {} ", JSONObject.toJSONString(jsonObjectList));
        return R.success(jsonObjectList.get(0));
    }

}
